<template>
  <div id="withdraw">
    <div class="page-header">
      <div class="balance">
        <i class="el-icon-ali-balance"></i>
        <span class="title">当前账号余额（{{ merchantMoneyText }}）：</span>
        <span class="money">{{ merchantCapital }}</span>
      </div>
      <div class="freeze-balance">
        <i class="el-icon-ali-freeze-balance"></i>
        <span class="title">当前冻结余额（{{ merchantMoneyText }}）：</span>
        <span class="money">{{ freezeMoney }}</span>
      </div>
    </div>
    <div class="card">
      <div class="card-header"><div class="title">填写你要提现的信息</div></div>
      <div class="card-content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
          <el-form-item>
            <el-form-item label="转账银行" prop="bank" v-show="this.checked == true" style="display: inline-block;">
              <el-input size="small" placeholder="请输入银行" v-model="ruleForm.bank"></el-input>
            </el-form-item>
            <el-form-item label="转账银行：" prop="bank" v-show="this.checked == false" style="display: inline-block;">
              <el-select v-model="ruleForm.bank" placeholder="请选择银行" size="small">
                <el-option label="中国银行" value="中国银行"></el-option>
                <el-option label="农业银行" value="农业银行"></el-option>
                <el-option label="建设银行" value="建设银行"></el-option>
                <el-option label="工商银行" value="工商银行"></el-option>
              </el-select>
            </el-form-item>
            <el-checkbox v-model="checked" style="margin-left:30px;">其他银行</el-checkbox>
          </el-form-item>
          <el-form-item label="银行卡卡号：" prop="bankNum"><el-input v-model="ruleForm.bankNum" placeholder="请输入银行卡卡号" size="small"></el-input></el-form-item>
          <el-form-item label="开户行：" prop="openBank"><el-input v-model="ruleForm.openBank" placeholder="请输入开户行" size="small"></el-input></el-form-item>
          <el-form-item label="开户人姓名：" prop="bankName"><el-input v-model="ruleForm.bankName" placeholder="请输入开户人姓名" size="small"></el-input></el-form-item>
          <el-form-item label="金额：" prop="money"><el-input v-model.number="ruleForm.money" type="number" placeholder="请输入金额" size="small"></el-input></el-form-item>
          <el-form-item><el-button :loading="submitLoading" class="withdraw-btn" @click="submitForm()">提现</el-button></el-form-item>
        </el-form>
      </div>
    </div>
    <div class="card">
      <div class="card-header"><div class="title">提现记录</div></div>
      <div class="card-content">
        <el-table :data="tableData" border size="mini">
          <el-table-column prop="bank" label="银行"></el-table-column>
          <el-table-column prop="bank_num" label="银行卡号"></el-table-column>
          <el-table-column prop="create_time" label="提现时间"></el-table-column>
          <el-table-column prop="open_bank" label="开户行"></el-table-column>
          <el-table-column prop="bank_name" label="开户人姓名"></el-table-column>
          <el-table-column prop="money_type" :formatter="getMoneyType" label="提现余额类型"></el-table-column>
          <el-table-column prop="money" :label="'提现余额' + '(' + merchantMoneyText + ')'"></el-table-column>
          <el-table-column prop="status" :formatter="getCheckStaus" label="审核状态"></el-table-column>
          <el-table-column prop="remark" label="信息"></el-table-column>
        </el-table>
        <div class="table-footer">
          <div></div>
          <el-pagination
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            :current-page="page"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageTotal"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiMerchantWithdraw, apiSubmitMerchantWithdraw } from '../../request/api.js';

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入转账金额'));
      } else {
        if (this.ruleForm.cobank !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.comoney) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        bank: '',
        bankNum: '',
        openBank: '',
        bank: '',
        moneyType: 1,
        money: '',
        token: ''
      },
      checked: false,
      tableData: [],
      page: 1,
      pageSize: 5,
      pageTotal: 0,
      merchantCapital: '',
      freezeMoney: '',
      rules: {
        bank: [
          {
            required: true,
            message: '请填写银行名称',
            trigger: 'change'
          }
        ],
        bankNum: [
          {
            required: true,
            message: '请填写银行卡号',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 20,
            message: '长度在 10 到 20 个字符',
            trigger: 'blur'
          }
        ],
        openBank: [
          {
            required: true,
            message: '请填写开户行',
            trigger: 'blur'
          }
        ],
        bankName: [
          {
            required: true,
            message: '请填写开户人姓名',
            trigger: 'blur'
          }
        ],
        money: [
          {
            required: true,
            message: '请填写金额',
            trigger: 'blur'
          }
        ]
      },
      checkStatus: {
        1: '审核成功',
        2: '审核失败',
        3: '未审核'
      },
      moneyType: {
        1: '本金',
        2: '佣金'
      },
      submitLoading: false,
      merchantMoneyText: window.localStorage.getItem('merchantMoneyText'),
      userToken: window.localStorage.getItem('token')
    };
  },
  created() {
    this.ruleForm.token = this.userToken;
    this.getTableData();
  },
  methods: {
    getCheckStaus(row) {
      return this.checkStatus[row.status];
    },
    getMoneyType(row) {
      return this.moneyType[row.money_type];
    },
    getTableData() {
      this.$isClick(true);
      apiMerchantWithdraw({
        page: this.page,
        pageSize: this.pageSize,
        token: this.userToken
      })
        .then(res => {
          this.$isClick(false);
          if (res.code == 1) {
            this.merchantCapital = res.msg.merchant_capital;
            this.freezeMoney = res.msg.freeze_money;
            this.tableData = res.msg.merchant_withdraw_list;
            this.pageTotal = res.msg.all_num;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(res => {
          this.$isClick(false);
          this.$message.error(res.msg);
        });
    },
    submitForm() {
      if (this.ruleForm.money == 0) {
        this.$message.error('金额不能为0');
        this.submitLoading = false;
        return;
      }
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.submitLoading = true;
          apiSubmitMerchantWithdraw(this.ruleForm)
            .then(res => {
              this.submitLoading = false;
              if (res.code == 1) {
                this.$refs['ruleForm'].resetFields();
                this.getTableData();
                return this.$message.success(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(res => {
              this.submitLoading = false;
              this.$message.error(res.msg);
            });
        } else {
          this.submitLoading = false;
        }
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    }
  }
};
</script>

<style lang="less">
#withdraw {
  .page-header {
    background: #ffffff;
    padding: 10px 0;
    margin-bottom: 16px;
    box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.1);
    div {
      display: inline-block;
      text-align: center;
      width: 50%;
    }
    .title {
      color: #666666;
      margin: 0 5px;
      font-size: 14px;
    }
    .money {
      color: @themeColor;
      font-size: 18px;
    }
    .balance {
      i {
        color: rgb(251, 193, 60);
        font-size: 20px;
        vertical-align: middle;
      }
    }
    .freeze-balance {
      i {
        color: red;
        font-size: 20px;
        vertical-align: middle;
      }
    }
  }
  .card {
    background: #ffffff;
    margin-bottom: 16px;
    box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.1);
    padding-bottom: 16px;
    border-radius: 5px;
    .card-header {
      background: #ecf5ff;
      padding: 10px;
      display: flex;
      align-items: center;
      margin-bottom: 16px;
      .title {
        line-height: 20px;
        border-left: 3px solid #666666;
        padding-left: 10px;
        font-size: 16px;
        font-weight: 700;
        color: #333333;
        width: 150px;
      }
    }
    .card-content {
      padding: 10px 20px;
      .el-table thead tr,
      .el-table thead tr th {
        background: #f5f7fa !important;
      }
      .el-table td,
      .el-table th {
        padding: 9px 0 !important;
      }
      .el-form-item__content {
        display: inline-block;
        margin-left: 0 !important;
        .el-input--small .el-input__inner {
          width: 200px;
        }
      }
      .table-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 10px;
        .download-btn {
          height: 35px !important;
          line-height: 35px;
          padding: 0px 20px !important;
          margin-left: 10px;
          font-size: 14px;
          color: #409eff;
          background: #ecf5ff;
          border-color: #b3d8ff;
        }
      }
      .withdraw-btn {
        width: 200px;
        background: @themeColor;
        padding: 10px 0;
        color: #ffffff;
        margin-left: 120px;
      }
    }
  }
}
</style>
